package com.yishun.springboot.dao;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.httpclient.Post8806;
import com.yishun.springboot.pojo.GenOneDataResults;
import com.yishun.springboot.pojo.GeneralRequestModel;

import lombok.extern.slf4j.Slf4j;

/**
 * 老余端
 * 
 * @author YS
 *
 */

@Slf4j
public class PostData {
	final static String BaseUrl = "http://47.99.64.49:2000";
	final static String ProjectId = "6620";

	final static String BeginTime = "2020-11-11 12:11:15";
	final static String setEndTime = new Date().toString();

	public static void main(String[] args) {
		Date d = new Date();
		long huansuan = System.currentTimeMillis() - 172680000;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 获取String类型的时间
		String huanhou = sdf.format(huansuan);
		log.info("huanhou:" + huanhou);

		// 请求体通用
//		Post8806 t = new Post8806();
//		GeneralRequestModel generalRequestModel = new GeneralRequestModel();
//		
//		//调用new一个
//		generalRequestModel.setProjectId(ProjectId);
//		generalRequestModel.setDeviceCode("000000YKDZHJJC0000000420");
//		generalRequestModel.setBeginTime("2020-11-11 12:11:15");
//		generalRequestModel.setEndTime("2020-11-21 13:11:15");
//		generalRequestModel.setPageNumber(2);
//		generalRequestModel.setPageNumber(20);
//		t.AllRequest0086("/Yc/GetData",generalRequestModel);
	}

	private GenOneDataResults PostThis(String AreaUrl, GeneralRequestModel generalRequestModel) {

		// 实例用来装载结果的结果集
		GenOneDataResults gr = new GenOneDataResults();

		// 类似浏览器转载的"容器"
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		// 拼接URL
		String Url = BaseUrl + AreaUrl;

		// 创建Post请求
		HttpPost httpPost = new HttpPost(Url);

		// 序列化通用请求体
		String jsonString = JSON.toJSONString(generalRequestModel);

		// 内容的编码对上UTF-8中文
		StringEntity entity = new StringEntity(jsonString, "UTF-8");

		// 将内容放进里面？
		httpPost.setEntity(entity);

		// 头部的编码对上UTF-8中文
		httpPost.setHeader("Content-Type", "application/json;charset=utf8");

		// 响应模型
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Post请求---------
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();

			String s = EntityUtils.toString(responseEntity);// 转换成字符串
			JSONObject Resultdatas = JSONObject.parseObject(s);// 转换成JSON格式
			String Success = Resultdatas.getString("Success");
			String Message = Resultdatas.getString("Message");
			String Data = Resultdatas.getString("Data");
			Integer Code = Integer.valueOf(Resultdatas.getString("Code"));

			log.info("Success:" + Success);
			log.info("Message:" + Message);
			log.info("Data:" + Data);
			log.info("Code:" + Code);
//        	Yc yc = new Yc();
//        	JSONObject jsonObject = JSONObject.fromObject(jsonStr);
//        	Map map = jsonObject;
//        	yc.setData(data);
//        	yc.setCode(Code);

			gr = JSONObject.toJavaObject(Resultdatas, GenOneDataResults.class);
			log.info("JSON对象转换成Java对象\n" + gr.getSuccess());
			log.info("JSON对象转换成Java对象\n" + gr.getMessage());
			log.info("JSON对象转换成Java对象\n" + gr.getCode());
			log.info("JSON对象转换成Java对象\n" + gr.getData());

			log.info("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				log.info("响应内容长度为:" + responseEntity.getContentLength());
				log.info("响应内容为:" + s);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return gr;

	}

}